using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SoftDemo.Application.Common.Interface;
using SoftDemo.Infrastructrue.File;
using SoftDemo.Infrastructrue.Identity;
using SoftDemo.Infrastructrue.Persistence;
using SoftDemo.Infrastructrue.Persistence.Repository;

namespace SoftDemo.Infrastructrue
{
    public static class DependencyInjection
    {
        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext<SoftDemoDbContext>(options =>
                options.UseNpgsql(
                    configuration.GetConnectionString("PostgreSQL"),
                    b => b.MigrationsAssembly(typeof(SoftDemoDbContext).Assembly.FullName)));

            services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
            services.AddTransient<IIdentityService, IdentityService>();
            services.AddScoped<IAppFileUploadServices,AppFileUploadServices>();

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    policy =>
                    {
                        policy.WithOrigins("http://localhost:8080").AllowAnyHeader().AllowAnyMethod();
                    });
            });

            return services;
        }
    }
}